package com.linewow.xhyy.nihaonew.view;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.linewow.xhyy.nihaonew.R;
import com.linewow.xhyy.nihaonew.entity.tag.CustomTagsInfo;

/**
 * Created by LXR on 2016/9/7.
 */
public class TagButton extends RelativeLayout {
    private TextView textView;
    private LayoutInflater layoutInflater;
    public TagButton(Context context) {
        super(context);
        init(context,null);
    }

    public TagButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);
    }
    public TagButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        layoutInflater= LayoutInflater.from(context);
        textView=new TextView(context);
        RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.setMargins(10,10,10,10);
        setBackgroundResource(R.drawable.bg_transparent_stroke_red);
        textView.setLayoutParams(lp);
        textView.setTextColor(ContextCompat.getColor(context,R.color.colorPrimary));
        addView(textView);
    }

    public void setCustomTagsInfo(CustomTagsInfo customTagsInfo){
        textView.setText(customTagsInfo.zhcn);
        if(!customTagsInfo.iscustom){
            if(customTagsInfo.color.startsWith("#")){
                try {
                    textView.setTextColor(Color.parseColor(customTagsInfo.color));
                }catch (NumberFormatException e){
//                    customTagsInfo.color=
                    textView.setTextColor(Color.parseColor("#FB586D"));
                }
            }
        }else{
            //自定义的button
            textView.setTextColor(Color.parseColor("#FB586D"));
        }
        setTag(customTagsInfo.tagid);
    }

    public void setTextViewHei(int hei){
        textView.setTextSize(hei);
    }
}
